﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class SupplierAdd : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName
        {
            get
            {
                return "MD_Supplier";
            }
        }
        private string m_strTbl = "MD_Supplier";
        #endregion
        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SupplierDetailID");
            dt.Columns.Add("SupplierID");
            dt.Columns.Add("ContactName");
            dt.Columns.Add("Tel");
            dt.Columns.Add("Mobile");
            dt.Columns.Add("Email");
            dt.Columns.Add("Job");
            return dt;
        }
        public DataTable SupplierDetailDT
        {
            get
            {
                if (Session["SupplierDetailDT"] != null)
                {
                    return (DataTable)Session["SupplierDetailDT"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["SupplierDetailDT"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SupplierDetailDT = null;
                BindDDL();
                this.lblSupplierID.Text = ClsComm.GetMaxSequenceID(m_strsequenceName, "GYS", 3);
            }
            Master.ValidationGroup = "gSubmit";
        }
        private void BindDDL()
        {
            BindParameterForListControl("SupplierScaler", ddlSupplierScaler);
            UIHelper.InsertFirstItem(ddlSupplierScaler);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写供应商资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.lblSupplierID.Text.Trim()))
                {
                    string name = this.txtSupplierName.Text.Trim();
                    string id = this.lblSupplierID.Text.Trim();
                    if (MdSupplier.CheckSupplierNameIsExists(name, id))
                    {
                        UIHelper.AlertMessage(this.Page, "供应商名称已存在，请重新输入");
                        return;
                    }
                    MdSupplierEntity entity = new MdSupplierEntity();
                    entity.SupplierID = id;
                    entity.SupplierName = name;
                    entity.SupplierScaler = this.ddlSupplierScaler.SelectedValue.Trim();
                    entity.Addr = this.txtAddr.Text.Trim();
                    entity.Status = "1";
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUserId = CurrentUser.UserID;
                    entity.CreateUserName = CurrentUser.UserName;

                    List<MdSupplierdetailEntity> list = new List<MdSupplierdetailEntity>();
                    foreach (GridViewRow gvr in this.grdDetail.Rows)
                    {
                        TextBox txtContactName = gvr.FindControl("txtContactName") as TextBox;
                        TextBox txtJob = gvr.FindControl("txtJob") as TextBox;
                        TextBox txtTel = gvr.FindControl("txtTel") as TextBox;
                        TextBox txtMobile = gvr.FindControl("txtMobile") as TextBox;
                        TextBox txtEmail = gvr.FindControl("txtEmail") as TextBox;

                        MdSupplierdetailEntity detail = new MdSupplierdetailEntity();
                        detail.SupplierID = entity.SupplierID;
                        detail.ContactName = txtContactName.Text.Trim();
                        detail.Job = txtJob.Text.Trim();
                        detail.Tel = txtTel.Text.Trim();
                        detail.Mobile = txtMobile.Text.Trim();
                        detail.Email = txtEmail.Text.Trim();
                        list.Add(detail);
                    }
                    if (list.Count < 1)
                    {
                        UIHelper.AlertMessage(this.UpdatePanel4, "请添加明细！");
                        return;
                    }
                    if (MdSupplier.SaveMdSupplierEntity(entity))
                    {
                        ClsComm.SetMaxSequenceID(m_strsequenceName);
                        MdSupplierdetail.Save(list);
                        base.LogAction(BaseEnum.ActionType.Add, entity.SupplierID, true, null, new EntityBase[] { entity });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/SupplierList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            GetGridViewData();
            DataTable dt = SupplierDetailDT.Copy();
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);

            SupplierDetailDT = dt;
            BindData();
        }
        private void BindData()
        {
            if (SupplierDetailDT.Rows.Count > 0)
            {
                this.grdDetail.DataSource = SupplierDetailDT;
                grdDetail.DataBind();
                SetControlAttr(new MdSupplierdetailEntity(), grdDetail);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdDetail, CreateDT());
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }
        private void GetGridViewData()
        {
            DataTable dttemp = CreateDT();
            foreach (GridViewRow gvr in grdDetail.Rows)
            {
                TextBox txtContactName = gvr.FindControl("txtContactName") as TextBox;
                TextBox txtTel = gvr.FindControl("txtTel") as TextBox;
                TextBox txtJob = gvr.FindControl("txtJob") as TextBox;
                TextBox txtMobile = gvr.FindControl("txtMobile") as TextBox;
                TextBox txtEmail = gvr.FindControl("txtEmail") as TextBox;


                DataRow row = dttemp.NewRow();
                row["ContactName"] = GetGridViewString(txtContactName);
                row["Tel"] = GetGridViewString(txtTel);
                row["Job"] = GetGridViewString(txtJob);
                row["Mobile"] = GetGridViewString(txtMobile);
                row["Email"] = GetGridViewString(txtEmail);


                dttemp.Rows.Add(row);
            }
            dttemp.AcceptChanges();
            SupplierDetailDT = dttemp;
        }
        protected void grdDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string name = e.CommandName;
            if (name == "del")
            {
                GetGridViewData();
                string str = e.CommandArgument.ToString();
                int iDel = CommonMethod.ConvertToInt(str);
                DataTable dt = SupplierDetailDT.Copy();
                dt.Rows.RemoveAt(iDel);
                SupplierDetailDT = dt.Copy();
                BindData();
                UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
            }
        }

        protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion
            }
        }
        protected void btnAddh_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}